package com.rong.raft.rpc.net;

import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;

import com.rong.raft.LeaderSelector;
import com.rong.raft.rpc.IRpcService;
import com.rong.raft.rpc.RpcServiceImpl;

/**
 * 
 * @desc rpc服务服务端
 *
 * @author liurong
 *
 * @date 2019年10月22日
 */
public class RpcServer {

	private String address;
	private int port;
	private LeaderSelector leaderSelector;

	public RpcServer(String address, LeaderSelector leaderSelector) {
		this.address = address;
		this.port = Integer.parseInt(address.split(":")[1]);
		this.leaderSelector = leaderSelector;
	}

	public void start() {
		try {
			LocateRegistry.createRegistry(port);
			IRpcService rpcService = new RpcServiceImpl(leaderSelector);
			Naming.bind("rmi://" + address + "/rpc", rpcService);
			System.out.println("rpc provider start success! address=" + address);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
